<?php

    class Admin_Model_ProviderMapper {
        public function _insert($name) {
            $data = array(
                'name' => $name
            );
            $table = new Application_Model_DbTable_Provider();
            $table->insert($data);

        }

        public function _getAll() {
            $link = mysql_connect('localhost', 'root', '');
            mysql_select_db('aitishop', $link);
            $query = "select * from provider";
            $result = mysql_query($query);
            $arr = array();
            while ($row = mysql_fetch_array($result)) {
                $provider = new Admin_Model_Provider($row['id'], $row['name']);
                $arr[] = $provider;
            }
            return $arr;
        }

        public function _getById($id) {
            $table = new Application_Model_DbTable_Provider();
            $row = $table->fetchRow('id = ' . $id);
            if ($row) {
                $provider = new Admin_Model_Provider($row->id, $row->name);
                return $provider;
            }
        }

        public function _delete($id) {
            $table = new Application_Model_DbTable_Provider();
            $row = $table->fetchRow('id=' . $id);
            if ($row) {
                $row->delete();
            }
        }

        public function _update($id, $name) {
            $table = new Application_Model_DbTable_Provider();
            $row = $table->fetchRow('id=' . $id);
            if ($row) {
                $row->name = $name;
                $row->save();
            }
        }

    }
